package com.smec.moblie.util;

import android.annotation.SuppressLint;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期工具
 *
 * @author kn
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil {

    /**
     * 不允许实例化
     */
    private DateUtil() {
    }

    /**
     * 根据value获取指定的日期（到月）
     *
     * @param nowDate    当前时间
     * @param dateFormat 日期格式，如yyyyMM
     * @param value      增减值
     * @return
     */
    public static String getYearMonthByCondition(Date nowDate,
                                                 String dateFormat, int value) {
        String ret = "";

        if (nowDate == null || dateFormat == null) {
            return ret;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(nowDate);

            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

            if (value == 0) {

            } else {
                cal.add(Calendar.MONTH, value);
            }

            ret = sdf.format(cal.getTime());

        }

        return ret;
    }

    /**
     * 获取当天日期 00:00:00
     *
     * @return
     */
    public static Date getToday() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        StringBuffer str = new StringBuffer().append(df.format(new Date()))
                .append(" 00:00:00");

        Date ret = null;

        try {
            ret = df.parse(str.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return ret;
    }

    /**
     * 获取当天日期 yyyyMMdd
     *
     * @return
     */
    public static String getTodayString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        return df.format(new Date());
    }

    /**
     * 获取上一天日期 yyyyMMdd
     *
     * @return
     */
    public static String getYesterday() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMMdd");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    /**
     * 获取当天时间
     *
     * @return
     */
    public static String getTimeNow() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ret = df.format(new Date());

        return ret;
    }

    /**
     * 获取指定月的第一天
     *
     * @return
     */
    public static Date getFirstDay(int year, int month) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date theDate = calendar.getTime();

        StringBuffer str = new StringBuffer().append(df.format(theDate))
                .append(" 00:00:00");

        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        Date d = null;
        try {
            d = df2.parse(str.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;

    }

    /**
     * 获取指定月的最后一天
     *
     * @return
     */
    public static Date getLastDay(int year, int month) {
        String str;
        Calendar calendar = new GregorianCalendar();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        SimpleDateFormat simpleFormate = new SimpleDateFormat("yyyy-MM-dd");
        str = simpleFormate.format(calendar.getTime());

        str = new StringBuffer().append(str).append(" 23:59:59").toString();

        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d = null;
        try {
            d = df2.parse(str.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return d;

    }

    public static String formatTimestamp(Timestamp timeStamp) {
        if (timeStamp == null) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm",
                    Locale.CHINA);
            return sdf.format(timeStamp);
        }
    }

    public static String formatTimestampforPost(Timestamp timestamp) {
        if (timestamp == null || "".equals(timestamp)) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",
                    Locale.CHINA);
            return sdf.format(timestamp);
        }
    }

    /**
     * 根据日期格式获取string
     *
     * @param date
     * @return yyyy/MM/dd
     */
    public static String getDateStringFromDate(Date date) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd",
                    Locale.CHINA);
            return sdf.format(date);
        } else {
            return "";
        }
    }

    /**
     * 处理timestamp数据，如果数据为1900-01-01，返回空; 否则返回
     *
     * @param timestamp
     * @return
     */
    @SuppressWarnings("deprecation")
    public static Timestamp handleDateForNull(Timestamp timestamp) {
        if (timestamp != null) {
            if (timestamp.getYear() == 0) {
                return null;
            } else {
                return timestamp;
            }

        } else {
            return null;
        }

    }

    /**
     * 是否为下午3点以后
     *
     * @return
     */
    public static boolean isAfterThreePM() {
        boolean ret = false;
        SimpleDateFormat df = new SimpleDateFormat("HHmmss");
        String now = df.format(new Date());
        int int1 = Integer.parseInt(now);
        if (int1 > 150000) {
            ret = true;
        }

        return ret;
    }
}
